<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>QtWebApp: httpserver/httplistener.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">QtWebApp
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('httplistener_8cpp_source.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">httplistener.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="httplistener_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160; </div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="httplistener_8h.html">httplistener.h</a>&quot;</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="httpconnectionhandler_8h.html">httpconnectionhandler.h</a>&quot;</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#include &quot;httpconnectionhandlerpool.h&quot;</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor">#include &lt;QCoreApplication&gt;</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160; </div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="keyword">using namespace </span>stefanfrings;</div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160; </div>
<div class="line"><a name="l00013"></a><span class="lineno"><a class="line" href="classstefanfrings_1_1HttpListener.html#a4f92377bf5d139ff3706a0b41fce1fc1">   13</a></span>&#160;<a class="code" href="classstefanfrings_1_1HttpListener.html#a4f92377bf5d139ff3706a0b41fce1fc1">HttpListener::HttpListener</a>(<span class="keyword">const</span> QSettings* settings, <a class="code" href="classstefanfrings_1_1HttpRequestHandler.html">HttpRequestHandler</a>* requestHandler, QObject *parent)</div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;    : QTcpServer(parent)</div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;{</div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;    Q_ASSERT(settings!=<span class="keyword">nullptr</span>);</div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;    Q_ASSERT(requestHandler!=<span class="keyword">nullptr</span>);</div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;    pool=<span class="keyword">nullptr</span>;</div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;    this-&gt;settings=settings;</div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;    this-&gt;requestHandler=requestHandler;</div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;    <span class="comment">// Reqister type of socketDescriptor for signal/slot handling</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    qRegisterMetaType&lt;tSocketDescriptor&gt;(<span class="stringliteral">&quot;tSocketDescriptor&quot;</span>);</div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;    <span class="comment">// Start listening</span></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    <a class="code" href="classstefanfrings_1_1HttpListener.html#a39cf1136caf6ba96554ef0a48fbff052">listen</a>();</div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;}</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160; </div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160; </div>
<div class="line"><a name="l00028"></a><span class="lineno"><a class="line" href="classstefanfrings_1_1HttpListener.html#abf6e9c9d9715a94d922a1d437e02ee6e">   28</a></span>&#160;<a class="code" href="classstefanfrings_1_1HttpListener.html#abf6e9c9d9715a94d922a1d437e02ee6e">HttpListener::~HttpListener</a>()</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;{</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;    <a class="code" href="classstefanfrings_1_1HttpListener.html#a5f5f2463036bc9a17f9fe8eb5c17c7f5">close</a>();</div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;    qDebug(<span class="stringliteral">&quot;HttpListener: destroyed&quot;</span>);</div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;}</div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160; </div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160; </div>
<div class="line"><a name="l00035"></a><span class="lineno"><a class="line" href="classstefanfrings_1_1HttpListener.html#a39cf1136caf6ba96554ef0a48fbff052">   35</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="classstefanfrings_1_1HttpListener.html#a39cf1136caf6ba96554ef0a48fbff052">HttpListener::listen</a>()</div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;{</div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    <span class="keywordflow">if</span> (!pool)</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;    {</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        pool=<span class="keyword">new</span> <a class="code" href="classstefanfrings_1_1HttpConnectionHandlerPool.html">HttpConnectionHandlerPool</a>(settings,requestHandler);</div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;    }</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;    QString host = settings-&gt;value(<span class="stringliteral">&quot;host&quot;</span>).toString();</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;    quint16 port=settings-&gt;value(<span class="stringliteral">&quot;port&quot;</span>).toUInt() &amp; 0xFFFF;</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;    QTcpServer::listen(host.isEmpty() ? QHostAddress::Any : QHostAddress(host), port);</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;    <span class="keywordflow">if</span> (!isListening())</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    {</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;        qCritical(<span class="stringliteral">&quot;HttpListener: Cannot bind on port %i: %s&quot;</span>,port,qPrintable(errorString()));</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;    }</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;        qDebug(<span class="stringliteral">&quot;HttpListener: Listening on port %i&quot;</span>,port);</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;    }</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;}</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160; </div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160; </div>
<div class="line"><a name="l00054"></a><span class="lineno"><a class="line" href="classstefanfrings_1_1HttpListener.html#a5f5f2463036bc9a17f9fe8eb5c17c7f5">   54</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="classstefanfrings_1_1HttpListener.html#a5f5f2463036bc9a17f9fe8eb5c17c7f5">HttpListener::close</a>() {</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;    QTcpServer::close();</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;    qDebug(<span class="stringliteral">&quot;HttpListener: closed&quot;</span>);</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    <span class="keywordflow">if</span> (pool) {</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;        <span class="keyword">delete</span> pool;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;        pool=<span class="keyword">nullptr</span>;</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;    }</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;}</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160; </div>
<div class="line"><a name="l00063"></a><span class="lineno"><a class="line" href="classstefanfrings_1_1HttpListener.html#a72530470ad6a62487b7b0d3d5c79d7b9">   63</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="classstefanfrings_1_1HttpListener.html#a72530470ad6a62487b7b0d3d5c79d7b9">HttpListener::incomingConnection</a>(<a class="code" href="httpconnectionhandler_8h.html#aac9d80ca2f2f3e2c74506d3b207b02d6">tSocketDescriptor</a> socketDescriptor) {</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;<span class="preprocessor">#ifdef SUPERVERBOSE</span></div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;    qDebug(<span class="stringliteral">&quot;HttpListener: New connection&quot;</span>);</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160; </div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;    <a class="code" href="classstefanfrings_1_1HttpConnectionHandler.html">HttpConnectionHandler</a>* freeHandler=<span class="keyword">nullptr</span>;</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    <span class="keywordflow">if</span> (pool)</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    {</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;        freeHandler=pool-&gt;<a class="code" href="classstefanfrings_1_1HttpConnectionHandlerPool.html#aa7d15239af996e1989960e9f09409bc2">getConnectionHandler</a>();</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;    }</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160; </div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    <span class="comment">// Let the handler process the new connection.</span></div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    <span class="keywordflow">if</span> (freeHandler)</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;    {</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;        <span class="comment">// The descriptor is passed via event queue because the handler lives in another thread</span></div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;        QMetaObject::invokeMethod(freeHandler, <span class="stringliteral">&quot;handleConnection&quot;</span>, Qt::QueuedConnection, Q_ARG(<a class="code" href="httpconnectionhandler_8h.html#aac9d80ca2f2f3e2c74506d3b207b02d6">tSocketDescriptor</a>, socketDescriptor));</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    }</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;    <span class="keywordflow">else</span></div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;    {</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;        <span class="comment">// Reject the connection</span></div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;        qDebug(<span class="stringliteral">&quot;HttpListener: Too many incoming connections&quot;</span>);</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;        QTcpSocket* socket=<span class="keyword">new</span> QTcpSocket(<span class="keyword">this</span>);</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;        socket-&gt;setSocketDescriptor(socketDescriptor);</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;        connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;        socket-&gt;write(<span class="stringliteral">&quot;HTTP/1.1 503 too many connections\r\nConnection: close\r\n\r\nToo many connections\r\n&quot;</span>);</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;        socket-&gt;disconnectFromHost();</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    }</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;}</div>
<div class="ttc" id="aclassstefanfrings_1_1HttpConnectionHandlerPool_html"><div class="ttname"><a href="classstefanfrings_1_1HttpConnectionHandlerPool.html">stefanfrings::HttpConnectionHandlerPool</a></div><div class="ttdoc">Pool of http connection handlers.</div><div class="ttdef"><b>Definition:</b> <a href="httpconnectionhandlerpool_8h_source.html#l00048">httpconnectionhandlerpool.h:48</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpConnectionHandlerPool_html_aa7d15239af996e1989960e9f09409bc2"><div class="ttname"><a href="classstefanfrings_1_1HttpConnectionHandlerPool.html#aa7d15239af996e1989960e9f09409bc2">stefanfrings::HttpConnectionHandlerPool::getConnectionHandler</a></div><div class="ttdeci">HttpConnectionHandler * getConnectionHandler()</div><div class="ttdoc">Get a free connection handler, or 0 if not available.</div><div class="ttdef"><b>Definition:</b> <a href="httpconnectionhandlerpool_8cpp_source.html#l00037">httpconnectionhandlerpool.cpp:37</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpConnectionHandler_html"><div class="ttname"><a href="classstefanfrings_1_1HttpConnectionHandler.html">stefanfrings::HttpConnectionHandler</a></div><div class="ttdoc">Alias for QSslConfiguration if OpenSSL is not supported.</div><div class="ttdef"><b>Definition:</b> <a href="httpconnectionhandler_8h_source.html#l00049">httpconnectionhandler.h:49</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpListener_html_a39cf1136caf6ba96554ef0a48fbff052"><div class="ttname"><a href="classstefanfrings_1_1HttpListener.html#a39cf1136caf6ba96554ef0a48fbff052">stefanfrings::HttpListener::listen</a></div><div class="ttdeci">void listen()</div><div class="ttdoc">Restart listeing after close().</div><div class="ttdef"><b>Definition:</b> <a href="httplistener_8cpp_source.html#l00035">httplistener.cpp:35</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpListener_html_a4f92377bf5d139ff3706a0b41fce1fc1"><div class="ttname"><a href="classstefanfrings_1_1HttpListener.html#a4f92377bf5d139ff3706a0b41fce1fc1">stefanfrings::HttpListener::HttpListener</a></div><div class="ttdeci">HttpListener(const QSettings *settings, HttpRequestHandler *requestHandler, QObject *parent=nullptr)</div><div class="ttdoc">Constructor.</div><div class="ttdef"><b>Definition:</b> <a href="httplistener_8cpp_source.html#l00013">httplistener.cpp:13</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpListener_html_a5f5f2463036bc9a17f9fe8eb5c17c7f5"><div class="ttname"><a href="classstefanfrings_1_1HttpListener.html#a5f5f2463036bc9a17f9fe8eb5c17c7f5">stefanfrings::HttpListener::close</a></div><div class="ttdeci">void close()</div><div class="ttdoc">Closes the listener, waits until all pending requests are processed, then closes the connection pool.</div><div class="ttdef"><b>Definition:</b> <a href="httplistener_8cpp_source.html#l00054">httplistener.cpp:54</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpListener_html_a72530470ad6a62487b7b0d3d5c79d7b9"><div class="ttname"><a href="classstefanfrings_1_1HttpListener.html#a72530470ad6a62487b7b0d3d5c79d7b9">stefanfrings::HttpListener::incomingConnection</a></div><div class="ttdeci">void incomingConnection(tSocketDescriptor socketDescriptor)</div><div class="ttdoc">Serves new incoming connection requests.</div><div class="ttdef"><b>Definition:</b> <a href="httplistener_8cpp_source.html#l00063">httplistener.cpp:63</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpListener_html_abf6e9c9d9715a94d922a1d437e02ee6e"><div class="ttname"><a href="classstefanfrings_1_1HttpListener.html#abf6e9c9d9715a94d922a1d437e02ee6e">stefanfrings::HttpListener::~HttpListener</a></div><div class="ttdeci">virtual ~HttpListener()</div><div class="ttdoc">Destructor.</div><div class="ttdef"><b>Definition:</b> <a href="httplistener_8cpp_source.html#l00028">httplistener.cpp:28</a></div></div>
<div class="ttc" id="aclassstefanfrings_1_1HttpRequestHandler_html"><div class="ttname"><a href="classstefanfrings_1_1HttpRequestHandler.html">stefanfrings::HttpRequestHandler</a></div><div class="ttdoc">The request handler generates a response for each HTTP request.</div><div class="ttdef"><b>Definition:</b> <a href="httprequesthandler_8h_source.html#l00027">httprequesthandler.h:27</a></div></div>
<div class="ttc" id="ahttpconnectionhandler_8h_html"><div class="ttname"><a href="httpconnectionhandler_8h.html">httpconnectionhandler.h</a></div></div>
<div class="ttc" id="ahttpconnectionhandler_8h_html_aac9d80ca2f2f3e2c74506d3b207b02d6"><div class="ttname"><a href="httpconnectionhandler_8h.html#aac9d80ca2f2f3e2c74506d3b207b02d6">stefanfrings::tSocketDescriptor</a></div><div class="ttdeci">qintptr tSocketDescriptor</div><div class="ttdoc">Alias type definition, for compatibility to different Qt versions.</div><div class="ttdef"><b>Definition:</b> <a href="httpconnectionhandler_8h_source.html#l00024">httpconnectionhandler.h:24</a></div></div>
<div class="ttc" id="ahttplistener_8h_html"><div class="ttname"><a href="httplistener_8h.html">httplistener.h</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_32a529a3b2babe504aaf73e401f143d0.html">httpserver</a></li><li class="navelem"><a class="el" href="httplistener_8cpp.html">httplistener.cpp</a></li>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
